<?php

class Admin_OtherController extends Zend_Controller_Action
{
	private $_params;
	private $_auth;
	private $_acl;
    public function init(){
        $this->_params      = $this->_request->getParams();
        $this->_acl         = new My_Acl();
        $this->view->acl    = $this->_acl;
        $this->_auth        = $this->_acl->checkAllowed();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelOther            = new Admin_Model_Other();
        $infoList             = $modelOther->getAllData()->toArray();
        $this->view->infoList = $infoList;
    }
    public function addAction(){
        $form = new Admin_Form_Estate();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        $modelOther          = new Admin_Model_Other();
        $info                   = $modelOther->getAllData()->toArray();        
        $this->view->otherInfo  = $info;                               
        if($this->getRequest()->isPost()){
            $formData = $this->getRequest()->getPost();            
            if($form->isValid($formData)){                             
                $addData['estateName'] = $formData['estateName'];                
                $addData['estateCode'] = $formData['estateCode'];
                $addData['sale']= $formData['sale'];
                $addData['rent']= $formData['rent'];
                
                $otherInfoT = $modelOther->getDataByGroup('otherInfo')->toArray();
                foreach($otherInfoT as $o) $otherInfoL[] = $o['id'];                
                $otherUtilT  = $modelOther->getDataByGroup('otherUtility')->toArray();
                foreach($otherUtilT as $o) $otherUtilL[] = $o['id'];                                                                    
                                                                
                $addData['otherInfo'] = json_encode(array_values(array_intersect($otherInfoL,$formData['info'])));                
                $addData['otherUtility'] = json_encode(array_values(array_intersect($otherUtilL,$formData['info'])));                
                                               
                $modelEstate = new Admin_Model_Estate();                               
                $estateCheck=$modelEstate->getDataCheckUnique(array('estateName'=>$addData['estateName'],'estateCode'=>$addData['estateCode']));
                                                                    
                if(!$estateCheck){                    
                    $estateId = $modelEstate->addData($addData);
                    if($estateId){
                        $this->_helper->redirector('index', 'estate', 'admin');
                    }else{
                        $this->view->mess = "Đã có lỗi xảy ra. Vui lòng thử lại!";
                        $this->view->flag = "error";
                   }
                }else{
                    $this->view->mess = "Tên hoặc Mã bất động sản đã tồn tại. Vui lòng chọn tên khác!";
                    $this->view->flag = "error";
                }
                
            }else{
                $this->view->mess = "Vui lòng điền đẩy đủ thông tin!";
                $this->view->flag = "error";
                $form->populate($formData);
            }
        }                       
    }
    public function editAction(){                        
        $estateId = (int)($this->_getParam('id', 0));
        $modelEstate = new Admin_Model_Estate();
        $modelOther = new Admin_Model_Other();                
        $estateCheck = $modelEstate->getDataByEstateId($estateId);        
        $form = new Admin_Form_Estate();
        $form->submit->setLabel('Lưu thay đổi');
        $this->view->form = $form;        
        $modelResource          = new Admin_Model_Other();
        $info                   = $modelResource->getAllData()->toArray();
        $this->view->otherInfo  = $info;        
        $this->view->estateCheck= $estateCheck;
        //pr($estateCheck);          
        if($estateCheck){
            if($this->getRequest()->isPost()){
                $formData = $this->getRequest()->getPost();
                if($form->isValid($formData)){
                    
                    $updateData['estateName'] = $formData['estateName'];                
                    $updateData['estateCode'] = $formData['estateCode'];                    
                    $updateData['sale']= $formData['sale'];
                    $updateData['rent']= $formData['rent'];             
                    
                    $otherInfoT =  $modelOther->getDataByGroup('otherInfo');
                    foreach($otherInfoT as $o) $otherInfoL[] = $o['id'];                       
                    $otherUtilityT =  $modelOther->getDataByGroup('otherUtility');
                    foreach($otherUtilityT as $o) $otherUtilityL[] = $o['id'];                    
                    if(isset($formData['info'])){
                        $updateData['otherInfo'] = json_encode(array_values(array_intersect($formData['info'],$otherInfoL)));                        
                        $updateData['otherUtility'] = json_encode(array_values(array_intersect($formData['info'],$otherUtilityL)));
                    }                                                                                                                                 
                    
                    $modelEstate = new Admin_Model_Estate();               
                    $estateCheck=$modelEstate->getDataCheckUnique(array('estateName'=>$updateData['estateName'],'estateCode'=>$updateData['estateCode']));
                    
                    if(!$estateCheck || $estateCheck['estateId'] == $estateCheck['estateId']){
                        $modelEstate->updateData($estateId,$updateData);
                        $this->_helper->redirector('index', 'estate', 'admin');
                    }else{
                        $this->view->mess = "Tên hoặc Mã bất động sản đã tồn tại. Vui lòng chọn tên khác!";
                        $this->view->flag = "error";
                    }                
                }else{
                    $form->populate($estateCheck->toArray());
                }
            }else{                                                
                $form->populate($estateCheck->toArray());                
            }
        }else{
            $this->_helper->redirector('index', 'estate', 'admin');
        }
    }
    public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
                $estateId = $this->getRequest()->getPost('estateId');
                $modelEstate = new Admin_Model_Estate();
                $modelEstate->deleteData($estateId); 
                // ghi log
            }
            $this->_helper->redirector('index', 'estate', 'admin');
        }else{
            $id = $this->_getParam('id', 0);
            $modelEstate = new Admin_Model_Estate();
            $this->view->estateCheck = $modelEstate->getDataByEstateId($id);            
        }
    }
    public function getestateAction(){
        $transaction = $this->_request->getParam('transaction');                        
        $modelEstate=new Admin_Model_Estate;
        $estateTemp=$modelEstate->getDataByTransaction($transaction);
        foreach($estateTemp as $estate){
            $estateList[$estate[estateId]]=$estate[estateName];
        }        
        echo json_encode($estateList);        
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);        
    }
    public function getunitAction(){
        $transaction = $this->_request->getParam('transaction');
        $conUnit=new Zend_Config_Ini(CONFIG_PATH.'/unit.ini',APPLICATION_ENV);
        $arr=$conUnit->item->unit->$transaction->toArray();
        echo json_encode($arr,JSON_UNESCAPED_UNICODE);
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);  
    }    
}





